package com.dede.net_speed_monitor.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.IntDef;
import android.telephony.TelephonyManager;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.NumberFormat;

/**
 * Created by hsh on 2017/5/15 015 下午 05:14.
 */
public class NetUtil {

    public static final int WIFI = 1;
    public static final int MOBILE = 2;
    public static final int MOBILE_2G = 3;
    public static final int MOBILE_3G = 4;
    public static final int MOBILE_4G = 5;
    public static final int NONE = -1;

    /**
     * 网络类型检查注解
     */
    @IntDef({WIFI, MOBILE, MOBILE_2G, MOBILE_3G, MOBILE_4G, NONE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface NetType {
    }

    /**
     * 获取网络状态
     *
     * @param context 上下文
     * @return NetType
     */
    @NetType
    public static int getNetState(Context context) {
        int state = NONE;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                state = WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                switch (activeNetworkInfo.getSubtype()) {
                    case TelephonyManager.NETWORK_TYPE_GSM:
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        state = MOBILE_2G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                        state = MOBILE_3G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_IWLAN:
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        state = MOBILE_4G;
                        break;
                    default:
                        String subtypeName = activeNetworkInfo.getSubtypeName();
                        if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                                || subtypeName.equalsIgnoreCase("WCDMA")
                                || subtypeName.equalsIgnoreCase("CDMA2000")) {
                            state = MOBILE_3G;
                        } else {
                            state = MOBILE;
                        }
                        break;
                }
            }
        } else {
            state = NONE;
        }
        return state;
    }

    public static String[] formatNetSpeed(long downloadSpeed) {
        double speed = downloadSpeed;
        String unit = "B/s";
        if (speed > 999) {
            speed /= 1024;
            unit = "Kb/s";
        }
        if (speed > 999) {
            speed /= 1024;
            unit = "Mb/s";
        }
        if (speed > 999) {
            speed /= 1024;
            unit = "Gb/s";
        }
        if (speed > 999) {
            speed /= 1024;
            unit = "Tb/s";
        }

        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(1);//小数点一位
        String format = numberFormat.format(speed);
        int length = format.length();
        if (length >= 4) {//100.2
            format = format.substring(0, length - 2);
        }
        String[] strings = new String[2];
        strings[0] = format;
        strings[1] = unit;
        return strings;
    }

    private NetUtil() {
    }
}
